package com.ssm.util.test.system;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import org.apache.commons.codec.digest.DigestUtils;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 获取cpu 主板  硬盘序列号
 * @author Administrator
 *
 */
public class MiscUtil {

	//获取cpu序列号
	@SuppressWarnings("resource")
	public static String getCpu()  throws Exception{
		  long start = System.currentTimeMillis();
		  Process process = Runtime.getRuntime().exec(
		    new String[] { "wmic", "cpu", "get", "ProcessorId" });
		  process.getOutputStream().close();
		  Scanner sc = new Scanner(process.getInputStream());
		  String property = sc.next();
		  String serial = sc.next();
		  System.out.println("获取cpu序列号:"+property + ": " + serial);
		  System.out.println("获取cpu序列号time(秒):" + (System.currentTimeMillis() - start));
		  return serial;
	}
	//获取主板序列号
	public static String getboardSN() throws Exception{
		  String result = "";
		   File file = File.createTempFile("realhowto", ".vbs");
		   file.deleteOnExit();
		   FileWriter fw = new java.io.FileWriter(file);

		   String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
		     + "Set colItems = objWMIService.ExecQuery _ \n"
		     + "   (\"Select * from Win32_BaseBoard\") \n"
		     + "For Each objItem in colItems \n"
		     + "    Wscript.Echo objItem.SerialNumber \n"
		     + "    exit for  ' do the first cpu only! \n" + "Next \n";

		   fw.write(vbs);
		   fw.close();
		   Process p = Runtime.getRuntime().exec(
		     "cscript //NoLogo " + file.getPath());
		   BufferedReader input = new BufferedReader(new InputStreamReader(p
		     .getInputStream()));
		   String line;
		   while ((line = input.readLine()) != null) {
		    result += line;
		   }
		   input.close();
		   System.out.println("获取主板序列号:"+result.trim());
		   return result.trim();
	}
	//硬盘序列号
	public static String getSerialNumber(String drive) throws Exception{
	    	String result = "";
	        File file = File.createTempFile("realhowto",".vbs");
	        file.deleteOnExit();
	        FileWriter fw = new java.io.FileWriter(file);

	        String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
	                    +"Set colDrives = objFSO.Drives\n"
	                    +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
	                    +"Wscript.Echo objDrive.SerialNumber";  // see note
	        fw.write(vbs);
	        fw.close();
	        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
	        BufferedReader input =
	          new BufferedReader
	            (new InputStreamReader(p.getInputStream()));
	        String line;
	        while ((line = input.readLine()) != null) {
	           result += line;
	        }
	        input.close();
	        System.out.println("获取主板序列号:"+result.trim());
	        return result.trim();
	    }
	//md5加密
	public static String sign(String text, String input_charset) {
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }
	private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (Exception e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }
	
	public static String getIP() throws Exception{
		  InetAddress addr = InetAddress.getLocalHost();
		  String ip = ip=addr.getHostAddress().toString();//获得本机IP
		  String address=addr.getHostName().toString();//获得本机IP
		  System.out.println("获取本地ip:"+ip+"--获取本地计算机名: "+address);
		  return ip;
	}
	
	public static void main(String[] args) throws Exception {
			getIP();
		  String boardId = MiscUtil.getboardSN();
		  String cpuId = getCpu();
		  String sn = getSerialNumber("C");
		  //使用本地序列号签名
		  String sign = sign(boardId, "utf-8");
		  System.out.println("签名:"+sign+ "--签名长度:"+sign.length());  //加密获得MD5 32位加密串

	}
	
	
}
